package com.xsoft.demo10.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.xsoft.demo10.DataBaseHelper;
import com.xsoft.demo10.GoodsInfo;
import com.xsoft.demo10.R;
import com.xsoft.demo10.SearchActivity;
import com.xsoft.demo10.TabInfo;
import com.xsoft.demo10.TabRecyclerAdapter;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

public class GoodsListFragment extends Fragment {
    private RecyclerView tabRecyclerView;
    private RecyclerView recyclerView;
    private GoodsListAdapter adapter;
    private DataBaseHelper dataBaseHelper;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_goods_list, container, false);
        view.findViewById(R.id.search).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(getContext(), SearchActivity.class));
            }
        });
        tabRecyclerView = view.findViewById(R.id.tab_recycler_view);
        tabRecyclerView.setLayoutManager(new LinearLayoutManager(getContext(), RecyclerView.HORIZONTAL, false));
        TabRecyclerAdapter tabRecyclerAdapter = new TabRecyclerAdapter();
        tabRecyclerView.setAdapter(tabRecyclerAdapter);
        tabRecyclerAdapter.bindData(initTabData());
        recyclerView = (RecyclerView) view.findViewById(R.id.recycler_view);
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 2);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);
        adapter = new GoodsListAdapter();
        recyclerView.setAdapter(adapter);
        dataBaseHelper = new DataBaseHelper(getContext());
        if (dataBaseHelper.query().isEmpty()) {
            initData();
        }
        adapter.bindData(dataBaseHelper.query());
        return view;
    }

    private void initData() {
        GoodsInfo info1 = new GoodsInfo();
        info1.setName("玉兰油护肤套装");
        info1.setDetail("Olay玉兰油 水感透白光塑钻纹弹力面膜");
        info1.setPrice(128.00f);
        info1.setPictureAlbum("olay.jpg");
        info1.setPicture("olay.jpg");
        dataBaseHelper.insert(info1);
        GoodsInfo info2 = new GoodsInfo();
        info2.setName("肌肤之食");
        info2.setDetail("肌肤之食 氨基酸洗面奶女男士泡沫洁面乳 敏感肌可用补水保湿清爽控油去黑头 去角质收缩毛孔深层清洁 氨基酸洗面奶230ml");
        info2.setPrice(69.90f);
        info2.setPictureAlbum("elate.jpg");
        info2.setPicture("elate.jpg");
        dataBaseHelper.insert(info2);
        GoodsInfo info3 = new GoodsInfo();
        info3.setName("马丁护肤三件套");
        info3.setDetail("马丁男士古龙香氛洗面奶面霜保湿水面部护肤三件套（爽肤水120ml+焕肤乳50ml+洁面乳150ml）");
        info3.setPrice(199.00f);
        info3.setPictureAlbum("martin.jpg");
        info3.setPicture("martin.jpg");
        dataBaseHelper.insert(info3);
        GoodsInfo info4 = new GoodsInfo();
        info4.setName("罗兰");
        info4.setDetail("罗兰（LUOLAN）罗兰香皂手工皂玫瑰精油皂128克清洁毛孔清爽控油洁面洗脸沐浴澡女香肥皂10盒 10盒玫瑰");
        info4.setPrice(93.80f);
        info4.setPictureAlbum("luolan.jpg");
        info4.setPicture("luolan.jpg");
        dataBaseHelper.insert(info4);
        GoodsInfo info5 = new GoodsInfo();
        info5.setName("悦诗风吟洁面");
        info5.setDetail("悦诗风吟 Innisfree 绿茶精萃保湿洗面奶洁面乳洁面膏 150mlx2只装（洗面奶男女 清爽不绷紧）");
        info5.setPrice(60.00f);
        info5.setPictureAlbum("innisfree.jpg");
        info5.setPicture("innisfree.jpg");
        dataBaseHelper.insert(info5);
        GoodsInfo info6 = new GoodsInfo();
        info6.setName("欧莱雅洁面乳");
        info6.setDetail("欧莱雅（LOREAL）清润葡萄籽水嫩净透光采氨基酸洁面乳125ml(洗面奶女 改善暗沉清洁肌肤 补水保湿 男女通用)");
        info6.setPrice(110.00f);
        info6.setPictureAlbum("loreal.jpg");
        info6.setPicture("loreal.jpg");
        dataBaseHelper.insert(info6);
        GoodsInfo info7 = new GoodsInfo();
        info7.setName("珂润洁面泡沫");
        info7.setDetail("日本珂润（Curel）润浸保湿洁颜泡沫 150ml（泡泡慕斯洁面乳温和敏感肌适用深层清洁男女士花王进口）");
        info7.setPrice(108.00f);
        info7.setPictureAlbum("curel.jpg");
        info7.setPicture("curel.jpg");
        dataBaseHelper.insert(info7);
        GoodsInfo info8 = new GoodsInfo();
        info8.setName("珀莱雅洁面乳");
        info8.setDetail("珀莱雅去角质啫喱面部身体去死皮膏男女去黑头洗面奶磨砂膏慕斯学生");
        info8.setPrice(35.00f);
        info8.setPictureAlbum("proya.jpg");
        info8.setPicture("proya.jpg");
        dataBaseHelper.insert(info8);
    }

    private List<TabInfo> initTabData() {
        List<TabInfo> infos = new ArrayList<>();
        TabInfo info1 = new TabInfo();
        info1.setTitle("首页");
        info1.setSelected(true);
        infos.add(info1);
        TabInfo info2 = new TabInfo();
        info2.setTitle("化妆品");
        info2.setSelected(false);
        infos.add(info2);
        TabInfo info3 = new TabInfo();
        info3.setTitle("数码");
        info3.setSelected(false);
        infos.add(info3);
        TabInfo info4 = new TabInfo();
        info4.setTitle("家装");
        info4.setSelected(false);
        infos.add(info4);
        TabInfo info5 = new TabInfo();
        info5.setTitle("生鲜");
        info5.setSelected(false);
        infos.add(info5);
        TabInfo info6 = new TabInfo();
        info6.setTitle("手机");
        info6.setSelected(false);
        infos.add(info6);
        TabInfo info7 = new TabInfo();
        info7.setTitle("家居");
        info7.setSelected(false);
        infos.add(info7);
        TabInfo info8 = new TabInfo();
        info8.setTitle("礼品");
        info8.setSelected(false);
        infos.add(info8);
        return infos;
    }
}
